package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.bo.ZhcaInformationAddBo;
import com.ruoyi.system.bo.ZhcaInformationEditBo;
import com.ruoyi.system.bo.ZhcaInformationQueryBo;
import com.ruoyi.system.domain.ZhcaInformation;
import com.ruoyi.system.mapper.ZhcaInformationMapper;
import com.ruoyi.system.service.IZhcaInformationService;
import com.ruoyi.system.vo.ZhcaInformationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 资讯管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-27
 */
@Service
public class ZhcaInformationServiceImpl extends ServicePlusImpl<ZhcaInformationMapper, ZhcaInformation> implements IZhcaInformationService {
	@Autowired
	private OfficialAccountsReptile happyChangAnReptile;
	@Autowired
	private OfficialAccountsReptile changAnEmergencyManagementReptile;
	@Autowired
	private OfficialAccountsReptile safeChangAnReptile;
	@Autowired
	private OfficialAccountsReptile happyWomanReptile;
	@Autowired
	private OfficialAccountsReptile changAnTownLibraryReptile;
	@Autowired
	private OfficialAccountsReptile changAnCultureAndEducationReptile;

	@Override
	public ZhcaInformationVo queryById(Long id) {
		return getVoById(id, ZhcaInformationVo.class);
	}

	@Override
	public TableDataInfo<ZhcaInformationVo> queryPageList(ZhcaInformationQueryBo bo) {
		PagePlus<ZhcaInformation, ZhcaInformationVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZhcaInformationVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<ZhcaInformationVo> queryList(ZhcaInformationQueryBo bo) {
		return listVo(buildQueryWrapper(bo), ZhcaInformationVo.class);
	}

	private LambdaQueryWrapper<ZhcaInformation> buildQueryWrapper(ZhcaInformationQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<ZhcaInformation> lqw = Wrappers.lambdaQuery();
		lqw.like(StrUtil.isNotBlank(bo.getTitle()), ZhcaInformation::getTitle, bo.getTitle());
		lqw.eq(StrUtil.isNotBlank(bo.getPic()), ZhcaInformation::getPic, bo.getPic());
		lqw.eq(StrUtil.isNotBlank(bo.getContent()), ZhcaInformation::getContent, bo.getContent());
		lqw.eq(bo.getState() != null, ZhcaInformation::getState, bo.getState());
		lqw.orderByDesc(ZhcaInformation::getId);
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(ZhcaInformationAddBo bo) {
		ZhcaInformation add = BeanUtil.toBean(bo, ZhcaInformation.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(ZhcaInformationEditBo bo) {
		ZhcaInformation update = BeanUtil.toBean(bo, ZhcaInformation.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(ZhcaInformation entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void reptileHappyChangAn() {
		List<ZhcaInformationAddBo> addBos = happyChangAnReptile.loadInformation();
		happyChangAnReptile.save(addBos);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void reptileChangAnEmergencyManagement() {
		List<ZhcaInformationAddBo> addBos = changAnEmergencyManagementReptile.loadInformation();
		changAnEmergencyManagementReptile.save(addBos);
	}

	@Override
	public void reptileSafeChangAn() {
		List<ZhcaInformationAddBo> addBos = safeChangAnReptile.loadInformation();
		safeChangAnReptile.save(addBos);
	}

	@Override
	public void reptileHappyWoman() {
		List<ZhcaInformationAddBo> addBos = happyWomanReptile.loadInformation();
		happyWomanReptile.save(addBos);
	}

	@Override
	public void reptileChangAnTownLibrary() {
		List<ZhcaInformationAddBo> addBos = changAnTownLibraryReptile.loadInformation();
		changAnTownLibraryReptile.save(addBos);
	}

	@Override
	public void reptileChangAnCultureAndEducation() {
		List<ZhcaInformationAddBo> addBos = changAnCultureAndEducationReptile.loadInformation();
		changAnCultureAndEducationReptile.save(addBos);
	}
}
